package reference;

/**
 * @author hujian
 * @date 2023/2/15 14:00
 * @description
 */

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReferenceChecker2 {
    public static void checkJournalReference(String reference) {
        String pattern = "^(?<authors>([^/]*?)(，[^/]*?)*?)\\.\\s+(?<title>[^/]*?)\\.(\\s+(?<journal>[^,]*?))\\s*,\\s*(?<year>\\d{4})\\s*,\\s*(?<volume>\\d+)(\\s*\\((?<issue>[^)]*?)\\))?\\s*:\\s*(?<pages>[^\\s]*?)(-[^\\s]*?)?\\s*\\.$";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(reference);
        if (m.find()) {
            String authors = m.group("authors");
            String title = m.group("title");
            String journal = m.group("journal");
            String year = m.group("year");
            String volume = m.group("volume");
            String issue = m.group("issue");
            String pages = m.group("pages");

            // 检查各个部分是否符合要求
            if (authors == null || authors.trim().equals("")) {
                System.out.println("缺少作者信息");
            }
            if (title == null || title.trim().equals("")) {
                System.out.println("缺少文章题目信息");
            }
            if (journal == null || journal.trim().equals("")) {
                System.out.println("缺少期刊名称信息");
            }
            if (year == null || year.trim().equals("") || !year.matches("\\d{4}")) {
                System.out.println("出版年份信息不正确");
            }
            if (volume == null || volume.trim().equals("")) {
                System.out.println("缺少卷号信息");
            }
            if (pages == null || pages.trim().equals("")) {
                System.out.println("缺少起止页码信息");
            }
        } else {
            System.out.println("参考文献不符合期刊文章格式要求");
        }
    }

    public static void checkThesisReference(String reference) {
        String pattern = "^(?<authors>([^/]*?)(，[^/]*?)*?)\\.\\s+(?<title>[^/]*?)\\[D\\]\\s*\\.\\s+(?<location>[^/]*?)：(?<school>[^/]*?),\\s*(?<year>\\d{4})\\s*\\.$";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(reference);
        if (m.find()) {
            String authors = m.group("authors");
            String title = m.group("title");
            String location = m.group("location");
            String school = m.group("school");
            String year = m.group("year");
// 检查各个部分是否符合要求
            if (authors == null || authors.trim().equals("")) {
                System.out.println("学位论文：缺少作者信息");
            }
            if (title == null || title.trim().equals("")) {
                System.out.println("学位论文：缺少论文题目信息");
            }
            if (location == null || location.trim().equals("")) {
                System.out.println("学位论文：缺少出版地信息");
            }
            if (school == null || school.trim().equals("")) {
                System.out.println("学位论文：缺少学位授予单位信息");
            }
            if (year == null || year.trim().equals("") || !year.matches("\\d{4}")) {
                System.out.println("学位论文：论文发表年份信息不正确");
            }
            System.out.println("========> 符合标准");
        } else {
            System.out.println("参考文献不符合学位论文格式要求");
        }
    }

    public static void checkPatentReference(String reference) {
        String pattern = "^(?<authors>([^/]*?)(，[^/]*?)*?)\\.\\s+(?<title>[^/]*?)\\s*\\.\\s+(?<country>[^/]*?):(?<patentNumber>[^\\s]*?)\\s*,\\s*(?<year>\\d{4})\\s*\\.$";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(reference);
        if (m.find()) {
            String authors = m.group("authors");
            String title = m.group("title");
            String country = m.group("country");
            String patentNumber = m.group("patentNumber");
            String year = m.group("year");
            // 检查各个部分是否符合要求
            if (authors == null || authors.trim().equals("")) {
                System.out.println("专利：缺少发明人信息");
            }
            if (title == null || title.trim().equals("")) {
                System.out.println("专利：缺少专利题目信息");
            }
            if (country == null || country.trim().equals("")) {
                System.out.println("专利：缺少专利申请国家信息");
            }
            if (patentNumber == null || patentNumber.trim().equals("")) {
                System.out.println("专利：缺少专利号信息");
            }
            if (year == null || year.trim().equals("") || !year.matches("\\d{4}")) {
                System.out.println("专利：专利公布年份信息不正确");
            }
        } else {
            System.out.println("参考文献不符合专利格式要求");
        }
    }

    public static void checkElectronicReference(String reference) {
        String pattern = "^(?<authors>([^/]*?)(，[^/]*?)*?)\\.\\s+(?<title>[^/]*?)\\s*\\[(?<type>[^\\]]*?)\\]\\s*\\[(?<medium>[^\\]]*?)\\]\\s*\\.\\s*(?<date>[^\\s]*?)\\s*\\((?<accessDate>[^\\)]*?)\\)\\s*\\.\\s*(?<url>[^\\s]*?)\\s*\\.$";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(reference);
        if (m.find()) {
            String authors = m.group("authors");
            String title = m.group("title");
            String type = m.group("type");
            String medium = m.group("medium");
            String date = m.group("date");
            String accessDate = m.group("accessDate");
            String url = m.group("url");
            // 检查各个部分是否符合要求
            if (authors == null || authors.trim().equals("")) {
                System.out.println("电子文献：缺少作者信息");
            }
            if (title == null || title.trim().equals("")) {
                System.out.println("电子文献：缺少文献题目信息");
            }
            if (type == null || type.trim().equals("")) {
                System.out.println("电子文献：缺少文献类型信息");
            }
            if (medium == null || medium.trim().equals("")) {
                System.out.println("电子文献：缺少介质类型信息");
            }
            if (date == null || date.trim().equals("")) {
                System.out.println("电子文献：缺少出版日期信息");
            }
            if (accessDate == null || accessDate.trim().equals("")) {
                System.out.println("电子文献：缺少访问日期信息");
            }
            if (url == null || url.trim().equals("")) {
                System.out.println("电子文献：缺少URL信息");
            }
        } else {
            System.out.println("参考文献不符合电子文献格式要求");
        }
    }


    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("D:\\Projectspace\\python\\individual\\pythonProjects2\\file_parse\\ref.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            if (str.contains("C:\\Users") || str.contains("参考文献")){
                continue;
            }
            checkThesisReference(str);
        }
//        System.out.println(str);
//        String reference = "[1]张雅楠. 短花针茅放牧适应性的转录组和蛋白质组学研究[D].内蒙古大学,2022.DOI:10.27224/d.cnki.gnmdu.2022.000476.";
//        checkThesisReference(reference);
    }
}
